module("core");

test("basic", function(){
    expect(2);
	alert(JsWorker.createWorkerFromUrl("workers/echo.js"));
    //ok(JsWorker.createWorkerFromUrl("workers/echo.js"), "createWorkerFromUrl");
    //ok(JsWorker.createWorkerFromText("function(){};"), "createWorkerFromText");
});

test("passing JSON object", function() {
	expect(1);
	var obj = {"test" : 123};
	var worker;
	function onmessage(msg) {
		if (!worker.isJSONMessageSupported()) {
			equals(msg.data, "[object Object]", "JSON messages are not supported.");
		}
		else {
			equals(msg.data.test, obj.test, "JSON object can be marshalled/unmarshalled between main thread and worker thread.");
		}
		start();
	}
	worker = JsWorker.createWorkerFromUrl("workers/echo.js", onmessage);
	alert(worker.getName());
	worker.postMessage(obj);
	stop();
});

test("worker-echo", function(){
	expect(1);
	var text = "test";
	function onmessage(msg) {
		equals(msg.data, text);
		start();
	}
	var worker = JsWorker.createWorkerFromUrl("workers/echo.js", onmessage);
	worker.postMessage(text);
	stop();
});

test("worker-adder", function() {
	expect(1);
	var worker;
	function onmessage(msg) {
		if (!worker.isJSONMessageSupported()) {
		  	equals(msg.data, "NaN", "Skip this test since JSON messages are not supported.");
		}
		else {
			equals(msg.data, 3);
		}
		start();
	}
	worker = JsWorker.createWorkerFromUrl("workers/adder.js", onmessage);
	worker.postMessage({"a" : 1, "b" : 2});
	stop();
});

test("worker-fib", function() {
	expect(1);
	function onmessage(msg) {
		equals(msg.data, 8);
		start();
	}
	var worker = JsWorker.createWorkerFromUrl("workers/fib.js", onmessage);
	worker.postMessage(5);
	stop();
});

test("worker-error", function() {
	expect(1);
	function onerror(error) {
		ok(error, "We need this error!");
		start();
	}
	function onmessage(message) {
		alert(message);
	}
	var worker = JsWorker.createWorkerFromUrl("workers/error.js", null, onerror);
	worker.postMessage("test");
	stop();
});

